/*
 * Copyright (c) 2011-2014, fortiss GmbH.
 * Licensed under the Apache License, Version 2.0.
 *
 * Use, modification and distribution are subject to the terms specified
 * in the accompanying license file LICENSE.txt located at the root directory
 * of this software distribution. A copy is available at
 * http://chromosome.fortiss.org/.
 *
 * This file is part of CHROMOSOME.
 *
 * $Id: plugAndPlayManagerProcessLogoutRequestsFunction.c 7837 2014-03-14 12:33:13Z wiesmueller $
 */

/**
 * \file
 *         Source file for function pnpManagerProcessLogoutRequests in component pnpManager.
 *
 * \author
 *         This file has been generated by the CHROMOSOME Modeling Tool (XMT)
 *         (fortiss GmbH).
 */

/******************************************************************************/
/***   Includes                                                             ***/
/******************************************************************************/
#include "xme/core/plugAndPlay/include-gen/plugAndPlayManagerProcessLogoutRequestsFunction.h"

#include "xme/core/plugAndPlay/include-gen/plugAndPlayManagerProcessLogoutRequestsFunctionWrapper.h"
#include "xme/core/plugAndPlay/include-gen/plugAndPlayManagerComponent.h"
#include "xme/core/plugAndPlay/include-gen/plugAndPlayManagerComponentWrapper.h"
#include "xme/core/plugAndPlay/include-gen/pnpManagerManifest.h"

#include "xme/core/logUtils.h"

#include "xme/hal/include/mem.h"

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSLOGOUTREQUESTSFUNCTION_C_INCLUDES) ENABLED START
#include "xme/core/plugAndPlay/include/plugAndPlayManager.h"
// PROTECTED REGION END

/******************************************************************************/
/***   Definitions                                                          ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSLOGOUTREQUESTSFUNCTION_C_DEFINITIONS) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Variables                                                            ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSLOGOUTREQUESTSFUNCTION_C_VARIABLES) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Prototypes                                                           ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSLOGOUTREQUESTSFUNCTION_C_PROTOTYPES) ENABLED START
/**
 * \brief Sends to Plug and Play Manager a logout request for a given node. 
 *
 * \param[in] nodeID The node identifier to remove from announcements. 
 *
 * \retval XME_STATUS_SUCCESS if the logout request was processed successfully, 
 *         and logout and remove runtime graphs were generated to be processed by
 *         the process runtime graphs function. 
 * \retval XME_STATUS_INVALID_PARAMETER if the provided node ID is an invalid node ID. 
 * \retval XME_STATUS_INTERNAL_ERROR if cannot process successfully the logout request. 
 */
xme_status_t
xme_core_pnp_pnpManager_processLogoutRequest
(
    xme_core_node_nodeId_t nodeID
);

/**
 * \brief Sends to the PnPManager the request to effectively remove all logical routes stored in LRM. 
 *
 * \param[in] nodeID The node identifier associated to the routes to be removed. 
 *
 * \retval XME_STATUS_SUCCESS if the logical routes removal is processed successfully. 
 * \retval XME_STATUS_INVALID_PARAMETER if the provided node ID is an invalid node ID. 
 * \retval XME_STATUS_INTERNAL_ERROR if cannot process successfully the logical routes' removal. 
 */
xme_status_t
xme_core_pnp_pnpManager_processLogoutACK
(
    xme_core_node_nodeId_t nodeID
);
// PROTECTED REGION END

/******************************************************************************/
/***   Implementation                                                       ***/
/******************************************************************************/
xme_status_t
xme_core_pnp_pnpManagerProcessLogoutRequestsFunction_init
(
    xme_core_pnp_pnpManagerComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSLOGOUTREQUESTSFUNCTION_INITIALIZE_C) ENABLED START

    // Nothing to do

    XME_UNUSED_PARAMETER(componentConfig);

    return XME_STATUS_SUCCESS;

    // PROTECTED REGION END
}

void
xme_core_pnp_pnpManagerProcessLogoutRequestsFunction_step
(
    xme_core_pnp_pnpManagerComponent_config_t* const componentConfig
)
{
    xme_status_t status[1];
    
    xme_core_topic_pnp_logoutRequest_t portInLogoutRequestData; // Optional port.
    
    (void)xme_hal_mem_set(&portInLogoutRequestData, 0u, sizeof(xme_core_topic_pnp_logoutRequest_t));
    
    status[0] = xme_core_pnp_pnpManagerComponentWrapper_readPortInLogoutRequest(&portInLogoutRequestData);
    
    {
        // PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSLOGOUTREQUESTSFUNCTION_STEP_C) ENABLED START

        XME_UNUSED_PARAMETER(componentConfig);

        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == status[0] ||
            XME_STATUS_NO_SUCH_VALUE == status[0],
            XME_CHECK_RVAL_VOID,
            XME_LOG_WARNING,
            "[PnPManager] Cannot read the input logout request.\n"
        );

        if (XME_STATUS_SUCCESS == status[0])
        {
            // There is data in the input logout request.
            XME_CHECK_MSG
            (
                XME_STATUS_SUCCESS == xme_core_pnp_pnpManager_processLogoutRequest(portInLogoutRequestData.nodeId),
                XME_CHECK_RVAL_VOID,
                XME_LOG_WARNING,
                "[PnPManager] Cannot process the logout request.\n"
            );
        }

        // FIXME: Remove as soon as the input port for receiving ACK is created.
#if 0
        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == status[1] ||
            XME_STATUS_NOT_FOUND == status[1],
            XME_CHECK_RVAL_VOID,
            XME_LOG_WARNING,
            "[PnPManager] Cannot read the input logout ACK.\n"
        );

        if (XME_STATUS_SUCCESS == status[1])
        {
            // There is data in the input logout ACK.
            XME_CHECK_MSG
            (
                XME_STATUS_SUCCESS == xme_core_pnp_pnpManager_processLogoutACK(portInPnPLogoutACKData.nodeId),
                XME_CHECK_RVAL_VOID,
                XME_LOG_WARNING,
                "[PnPManager] Cannot process the logout request.\n"
            );
        }
#endif

        // PROTECTED REGION END
    }
    
    {
        // PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSLOGOUTREQUESTSFUNCTION_STEP_2_C) ENABLED START

        // Nothing to do

        // PROTECTED REGION END
    }
}

void
xme_core_pnp_pnpManagerProcessLogoutRequestsFunction_fini
(
    xme_core_pnp_pnpManagerComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSLOGOUTREQUESTSFUNCTION_TERMINATE_C) ENABLED START

    // Nothing to do

    XME_UNUSED_PARAMETER(componentConfig);

    // PROTECTED REGION END
}

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSLOGOUTREQUESTSFUNCTION_IMPLEMENTATION_C) ENABLED START
xme_status_t
xme_core_pnp_pnpManager_processLogoutRequest
(
    xme_core_node_nodeId_t nodeID
)
{
    xme_status_t unannounceNodeStatus;

    unannounceNodeStatus = xme_core_pnp_pnpManager_unannounceNode(nodeID);

    XME_CHECK_MSG
    (
        XME_STATUS_SUCCESS == unannounceNodeStatus,
        unannounceNodeStatus,
        XME_LOG_ERROR,
        "[PnPManager] Cannot generate logout runtime graphs for node %d.\n", nodeID
    );

    return XME_STATUS_SUCCESS;
}

xme_status_t
xme_core_pnp_pnpManager_processLogoutACK
(
    xme_core_node_nodeId_t nodeID
)
{
    xme_status_t removeRoutesStatus;

    removeRoutesStatus = xme_core_pnp_pnpManager_removeNodeInstances(nodeID);

    XME_CHECK_MSG
    (
        XME_STATUS_SUCCESS == removeRoutesStatus,
        removeRoutesStatus,
        XME_LOG_ERROR,
        "[PnPManager] Cannot remove logical routes for node %d.\n", nodeID
    );

    return XME_STATUS_SUCCESS;
}

// PROTECTED REGION END
